package com.lanou3g.dllo.baidumusic.musiclibrary.radio;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.MyApp;
import com.lanou3g.dllo.baidumusic.tools.MyRvOnClickListener;
import com.lanou3g.dllo.baidumusic.tools.SingleVolley;

import java.util.List;

/**
 * Created by dllo on 16/6/25.
 */

public class RadioDetailAdapter extends RecyclerView.Adapter<RadioDetailAdapter.MyViewHolder>{

    private RadioDetailBean radioDetailBean;
    private List<RadioDetailBean.ResultBean> list;
    private MyRvOnClickListener myRvOnClickListener;

    public void setMyRvOnClickListener(MyRvOnClickListener myRvOnClickListener) {
        this.myRvOnClickListener = myRvOnClickListener;
    }

    public void setRadioDetailBean(RadioDetailBean radioDetailBean) {
        this.radioDetailBean = radioDetailBean;
        list = radioDetailBean.getResult();
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MyApp.context).inflate(R.layout.item_radiodetail,parent,false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.radioDetailTextTv.setText(list.get(position).getScene_name());
        SingleVolley.getSingleVolley(MyApp.context).getImageLoader().get(list.get(position)
                .getIcon_android(), ImageLoader.getImageListener(holder.radioDetailPictureIv,R.mipmap.ic_launcher,R.mipmap.ic_launcher));
        holder.radioDetailLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 当itemView被点击的时候
                // getLayoutPosition是用来获得当前点击的position
                int position = holder.getLayoutPosition();
                // 调用接口对象的onClick方法
                myRvOnClickListener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView radioDetailPictureIv;
        TextView radioDetailTextTv;
        LinearLayout radioDetailLayout;

        public MyViewHolder(View itemView) {
            super(itemView);
            radioDetailPictureIv = (ImageView) itemView.findViewById(R.id.radiodetail_picture_iv);
            radioDetailTextTv = (TextView) itemView.findViewById(R.id.radiodetail_text_tv);
            radioDetailLayout = (LinearLayout) itemView.findViewById(R.id.radiodetail_layout);

        }
    }
}
